<?php

namespace common\models\AdminLog;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\web\IdentityInterface;
use common\models\user\User;
use common\models\Setting\Setting;
/**
 * This is the model class for table "admin_log".
 *
 * @property string $log_id
 * @property string $log_time
 * @property integer $user_id
 * @property string $log_info
 * @property string $ip_address
 */
class AdminLog extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin_log';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['log_time', 'user_id'], 'integer'],
            [['log_info'], 'string', 'max' => 255],
            [['ip_address'], 'string', 'max' => 15],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'log_id' => Yii::t('app', '日志id'),
            'log_time' => Yii::t('app', '操作时间'),
            'user_id' => Yii::t('app', '用户名'),
            'log_info' => Yii::t('app', '操作行为'),
            'ip_address' => Yii::t('app', 'Ip地址'),
        ];
    }
	
	public static function getUserName($uid){
		
		$uinfo = User::findBySql('select username from user where id='.$uid)->asArray()->one();
		return $uinfo['username'];
	}

    public static function addlog($user,$action){
        $log = new Adminlog();
        $log->user_id=$user;
        $log->log_time = time();
        $log->log_info = $action;
        $log->ip_address = $log->real_ip();
		$setting = Setting::findBySql('SELECT value FROM setting where id = 38')->asArray()->all();
		if($setting[0]['value'] == 'yes'){
			$log->save();
		}
    }
	
	public function real_ip()
	{
		if(!empty($_SERVER["HTTP_CLIENT_IP"])){
			$cip = $_SERVER["HTTP_CLIENT_IP"];
		}
		elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
			$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
		}
		elseif(!empty($_SERVER["REMOTE_ADDR"])){
			$cip = $_SERVER["REMOTE_ADDR"];
		}else{
			$cip = "Unknown";
		}
		return $cip;
	}
}
